////////////////////////////////////////////////////////////////////////////////////////
//
// JSimpleSim - A machine language simulator based on SimpSim by Anne-Gert Bultena.
//
// Copyright (C) 2008 Gerardo Camorlinga, Ryan Fitzgerald, and RJ Gordon
//
// This file is part of JSimpleSim.
//
// JSimpleSim is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// JSimpleSim is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with JSimpleSim.  If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////////////

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsimplesim.view.renderers;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

/**
 *
 * @author f08maroon
 */
public class HighlightCellRenderer extends DefaultTableCellRenderer {

    /**
	 * 
	 */
	private static final long serialVersionUID = -7865602821690434453L;
	private static final Color hexBackground = new Color(212, 208, 200);
    //private static final Color ansBack = new Color(236, 233, 216);
    final Border rightBorder = BorderFactory.createMatteBorder(0, 0, 0, 2, Color.black);
    final Border leftBorder = BorderFactory.createMatteBorder(0, 2, 0, 0, Color.black);
    private int textRow;
    private int textColumn;
    private int tableRowHeight;
    private int tableRow;
    private int tableColumn;

    @Override
    public Component getTableCellRendererComponent(JTable table,
            Object value,
            boolean isSelected,
            boolean hasFocus,
            int row,
            int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        textRow = row;
        textColumn = column;
        tableRowHeight = table.getRowHeight();

        //  Update when the header row is not selected

        if (table.getSelectedRow() != -1) {
            tableRow = table.getSelectedRow();
            tableColumn = table.getSelectedColumn();
        }

        if (column == 0) // Address
        {
            setHorizontalAlignment(RIGHT);
            setBorder(rightBorder);
            setOpaque(false);
            this.setBackground(hexBackground);
        } else // Text
        {
            setHorizontalAlignment(LEFT);
            //setBorder( leftBorder );
            setOpaque(false);
            this.setBackground(hexBackground);
        }

        return this;
    }

    @Override
    public void paintComponent(Graphics g) {
        //  Add background highlighting when same row as selected cell row

        if (textRow == tableRow) {
            g.setColor(hexBackground);

            if (textColumn == 0) // Address
            {
                Dimension d = getSize();
                g.fillRect(0, 0, d.width, d.height);
            } else // Text
            {
                FontMetrics fontMetrics = g.getFontMetrics();
                int width = fontMetrics.charWidth('w');
                int start = width * tableColumn;
                g.fillRect(2 + start, 0, width, tableRowHeight);
            }

            g.setColor(getForeground());
        }

        super.paintComponent(g);
    }
}

